package pl.vanta.mjuzik2.impl.player.formats.mp3;

import java.util.Map;

import javax.sound.sampled.AudioFileFormat;

import org.tritonus.share.sampled.file.TAudioFileFormat;

import pl.vanta.mjuzik2.impl.player.formats.AbstractFileInfo;

public class Mp3FileInfo extends AbstractFileInfo
{
	public static final String	MP3_CHANNELS			= "mp3.channles";
	public static final String	MP3_FREQUENCY_HZ		= "mp3.frequency.hz";
	public static final String	MP3_BITRATE_NOMINAL_BPS	= "mp3.bitrate.nominal.bps";
	public static final String	MP3_VERSION_LAYER		= "mp3.version.layer";
	public static final String	MP3_VERSION_MPEG		= "mp3.version.mpeg";
	public static final String	MP3_MODE				= "mp3.mode";
	public static final String	MP3_CRC					= "mp3.crc";
	public static final String	MP3_VBR					= "mp3.vbr";
	public static final String	MP3_COPYRIGHT			= "mp3.copyright";
	public static final String	MP3_ORIGINAL			= "mp3.original";

	public static final String	TITLE					= "title";
	public static final String	AUTHOR					= "author";
	public static final String	ALBUM					= "album";
	public static final String	DATE					= "date";
	public static final String	DURATION				= "duration";
	public static final String	MP3_ID3TAG_GENRE		= "mp3.id3tag.genre";
	public static final String	MP3_ID3TAG_TRACK		= "mp3.id3tag.track";

	private String				version;
	private String				layer;
	private String				channelsMode;
	private boolean				crc;
	private boolean				vbr;
	private boolean				copyright;
	private boolean				original;

	public Mp3FileInfo()
	{
		this.propertiesDescription.put(MP3_CHANNELS, "Number of channels");
		this.propertiesDescription.put(MP3_FREQUENCY_HZ, "Frequency (Hz)");
		this.propertiesDescription.put(MP3_BITRATE_NOMINAL_BPS, "Nominal bitrate (bps)");
		this.propertiesDescription.put(MP3_VERSION_LAYER, "Version");
		this.propertiesDescription.put(MP3_VERSION_MPEG, "Mpeg");
		this.propertiesDescription.put(MP3_MODE, "Channels mode");
		this.propertiesDescription.put(MP3_CRC, "CRC");
		this.propertiesDescription.put(MP3_VBR, "VBR");
		this.propertiesDescription.put(MP3_COPYRIGHT, "Copyright");
		this.propertiesDescription.put(MP3_ORIGINAL, "Original");

	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean loadFormatSpecificInfo(AudioFileFormat format)
	{
		if (format instanceof TAudioFileFormat)
		{
			TAudioFileFormat audioFileFormat = (TAudioFileFormat) format;
			Map props = audioFileFormat.properties();

			this.properties.putAll(props);

			if (props.containsKey(MP3_CHANNELS))
				this.channels = ((Integer) props.get(MP3_CHANNELS)).intValue();
			if (props.containsKey(MP3_FREQUENCY_HZ))
				this.samplingRate = ((Integer) props.get(MP3_FREQUENCY_HZ)).intValue();
			if (props.containsKey(MP3_BITRATE_NOMINAL_BPS))
				this.bitRate = ((Integer) props.get(MP3_BITRATE_NOMINAL_BPS)).intValue();
			if (props.containsKey(MP3_VERSION_LAYER))
				this.layer = "Layer " + props.get(MP3_VERSION_LAYER);

			if (props.containsKey(MP3_VERSION_MPEG))
			{
				this.version = (String) props.get(MP3_VERSION_MPEG);
				if (this.version.equals("1"))
					this.version = "MPEG1";
				else if (this.version.equals("2"))
					this.version = "MPEG2-LSF";
				else if (this.version.equals("2.5"))
					this.version = "MPEG2.5-LSF";
			}

			if (props.containsKey(MP3_MODE))
			{
				int mode = ((Integer) props.get(MP3_MODE)).intValue();
				if (mode == 0)
					this.channelsMode = "Stereo";
				else if (mode == 1)
					this.channelsMode = "Joint Stereo";
				else if (mode == 2)
					this.channelsMode = "Dual Channel";
				else if (mode == 3)
					this.channelsMode = "Single Channel";
			}

			if (props.containsKey(MP3_CRC))
				this.crc = ((Boolean) props.get(MP3_CRC)).booleanValue();
			if (props.containsKey(MP3_VBR))
				this.vbr = ((Boolean) props.get(MP3_VBR)).booleanValue();
			if (props.containsKey(MP3_COPYRIGHT))
				this.copyright = ((Boolean) props.get(MP3_COPYRIGHT)).booleanValue();
			if (props.containsKey(MP3_ORIGINAL))
				this.original = ((Boolean) props.get(MP3_ORIGINAL)).booleanValue();

			if (props.containsKey(TITLE))
				this.title = (String) props.get(TITLE);
			if (props.containsKey(AUTHOR))
				this.artist = (String) props.get(AUTHOR);
			if (props.containsKey(ALBUM))
				this.album = (String) props.get(ALBUM);
			if (props.containsKey(DATE))
				this.year = (String) props.get(DATE);
			if (props.containsKey(DURATION))
				this.playtime = Math.round((((Long) props.get(DURATION)).longValue()) / 1000);
			if (props.containsKey(MP3_ID3TAG_GENRE))
				this.genre = (String) props.get(MP3_ID3TAG_GENRE);

			if (props.containsKey(MP3_ID3TAG_TRACK))
			{
				try
				{
					this.track = Integer.parseInt((String) props.get(MP3_ID3TAG_TRACK));
				}
				catch (NumberFormatException e1)
				{
					// Not a number
				}
			}

			return true;
		}
		else
		{
			return false;
		}
	}

	public String getVersion()
	{
		return this.version;
	}

	public String getLayer()
	{
		return this.layer;
	}

	public String getChannelsMode()
	{
		return this.channelsMode;
	}

	public boolean isCrc()
	{
		return this.crc;
	}

	public boolean isVbr()
	{
		return this.vbr;
	}

	public boolean isCopyright()
	{
		return this.copyright;
	}

	public boolean isOriginal()
	{
		return this.original;
	}
}